package com.itheima.socketWork01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class ClientUdp {
    public static void main(String[] args) throws IOException {
       //创建DatagramSocket对象，UDP是无连接发送，所以无需端口号
        DatagramSocket ds = new DatagramSocket();
        //获取键盘录入的数据
        Scanner sc = new Scanner(System.in);

        while (true){
            String str = sc.next();
            //定义一个结束的标识，比如输入“over”结束获取
            if ("over".equals(str)){
                break;
            }
            //、将键盘录入的数据打包DatagramPacket，打包时指定数据字节、数据字节的长度、发送目的地、端口号
            byte[] bytes = str.getBytes();
            InetAddress byName = InetAddress.getByName("127.0.0.1");
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length,byName,10086);
          //使用DatagramSocket对象发送数据包
            ds.send(dp);
        }
            ds.close();

    }
}
